//
//  LYCreditEvaluationView.m
//  Btpin
//
//  Created by liuyan on 2019/4/9.
//  Copyright © 2019年 Vic. All rights reserved.
//

#define kDefaultLineColor [UIColor whiteColor]
#define kDefaultFillLineColor RGB(0, 190, 90)

//进度条距离外部边缘的距离
#define kCompassSpace 10

//开始弧度
#define kStartAngle (-M_PI)

//结束弧度
#define kEndAngle (0)

#import "LYCreditEvaluationView.h"

@interface LYCreditEvaluationView()

@property(nonatomic) CGFloat needleRadius;
@property(nonatomic) CGFloat currentRadian;
@property(nonatomic, strong) UIImageView *centerImage;

@end


@implementation LYCreditEvaluationView

-(UIImageView *)centerImage{
    if (!_centerImage) {
        _centerImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
        _centerImage.layer.cornerRadius = _centerImage.height/2;
        _centerImage.clipsToBounds = YES;
        [self addSubview:_centerImage];
    }
    return _centerImage;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        self.lineColor = kDefaultLineColor;
        self.fillLineColor = kDefaultFillLineColor;
        self.lineWidth = 25;
        self.maximumValue = 100;
        self.currentRadian = 0;
    }
    return self;
}

- (void)setValue:(CGFloat)value{
    _value = value;
    [self setNeedsDisplay];
}

- (void)setLineWidth:(CGFloat)lineWidth{
    _lineWidth = lineWidth;
    [self setNeedsDisplay];
}

- (void)setLineColor:(UIColor *)lineColor{
    _lineColor = lineColor;
    [self setNeedsDisplay];
}

- (void)setFillLineColor:(UIColor *)fillLineColor{
    _fillLineColor = fillLineColor;
    [self setNeedsDisplay];
}

- (void)layoutSubviews{
    [super layoutSubviews];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, self.lineWidth);
    CGFloat lenghts[2] = {4, 4};
    CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
    CGContextSetLineDash(context, 0, lenghts, sizeof(lenghts)/sizeof(lenghts[0]));
    CGContextAddArc(context, self.frame.size.width/2, self.frame.size.height/2, MIN(self.frame.size.width, self.frame.size.height)/2 - kCompassSpace - self.lineWidth/2, kStartAngle, kEndAngle, NO);
    CGContextStrokePath(context);
    
    /**
     *  画外形圆圈
     */
    CGContextSetLineWidth(context, 3);
    CGContextSetLineDash(context, 0, NULL, 0);
    CGContextSetStrokeColorWithColor(context, self.fillLineColor.CGColor);
    CGContextStrokePath(context);
    CGFloat progess = self.value/self.maximumValue;
    NSString *progesstext = [NSString stringWithFormat:@"%0.0f",progess*100];
    _block(progesstext);
    self.centerImage.backgroundColor = [UIColor whiteColor];
    _centerImage.center = CGPointMake(self.width/2, self.height/2);
    self.currentRadian = progess*100;
    
    /**
     *  画进度条
     */
    CGContextSetLineWidth(context, self.lineWidth);
    if (self.value <= self.maximumValue){
        CGFloat endArc = M_PI* progess;
        CGContextSetLineDash(context, 0, lenghts, sizeof(lenghts)/sizeof(lenghts[0]));
        CGContextAddArc(context, self.frame.size.width/2, self.frame.size.height/2, MIN(self.frame.size.width, self.frame.size.height)/2 - kCompassSpace - self.lineWidth/2, kStartAngle, -M_PI + endArc, NO);
    }else{
        CGContextSetLineDash(context, 0, lenghts, sizeof(lenghts)/sizeof(lenghts[0]));
        CGContextAddArc(context, self.frame.size.width/2, self.frame.size.height/2, MIN(self.frame.size.width, self.frame.size.height)/2 - kCompassSpace - self.lineWidth/2, kStartAngle, kEndAngle, NO);
    }
    CGContextStrokePath(context);
    
    [self drawNeedle];
}

//画指针
-(void) drawNeedle{
    CGFloat distance = self.width/2 * 0.7 ;
    CGFloat starttime = - M_PI;
    CGFloat endtime = 0;
    //画贝塞尔
    CGPoint origin = [self needleOrigin];
    CGPoint topPoint_2 = CGPointMake(origin.x - distance, origin.y - 0.8);
    CGPoint topPoint_1 = CGPointMake(origin.x - distance, origin.y + 0.8);
    CGPoint finishPoint = CGPointMake(origin.x, origin.y - self.needleRadius);
    UIBezierPath *needlePath = [UIBezierPath bezierPath];
    [needlePath moveToPoint:origin];
    CGPoint next = finishPoint;
    
    [needlePath addLineToPoint:next];
    [needlePath addArcWithCenter:origin radius:self.needleRadius startAngle:starttime endAngle:endtime clockwise:YES];
    [needlePath addLineToPoint:topPoint_1];
    [needlePath addLineToPoint:topPoint_2];
    [needlePath addLineToPoint:finishPoint];
    //设置位移旋转
    CGAffineTransform translate = CGAffineTransformMakeTranslation(-1 * (self.bounds.origin.x + [self needleOrigin].x), -1 * (self.bounds.origin.y + [self needleOrigin].y));
    [needlePath applyTransform:translate];
    
    CGAffineTransform rotate = CGAffineTransformMakeRotation(self.currentRadian/32);
    [needlePath applyTransform:rotate];
    
    translate = CGAffineTransformMakeTranslation((self.bounds.origin.x + [self needleOrigin].x), (self.bounds.origin.y + [self needleOrigin].y));
    [needlePath applyTransform:translate];
    [[UIColor whiteColor] set];
    [needlePath fill];
}

-(CGFloat)needleRadius{
    return self.bounds.size.width * 0.015;
}

-(CGPoint)needleOrigin{
    return CGPointMake(self.width/2, self.height/2);
}

@end
